/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.modules.cspimp.psp.service.impl;

import me.zhengjie.modules.cspimp.psp.domain.OmBaseInfo;
import me.zhengjie.modules.cspimp.psp.domain.vo.OmBaseInfoVo;
import me.zhengjie.utils.*;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.cspimp.psp.repository.OmBaseInfoRepository;
import me.zhengjie.modules.cspimp.psp.service.OmBaseInfoService;
import me.zhengjie.modules.cspimp.psp.service.dto.OmBaseInfoDto;
import me.zhengjie.modules.cspimp.psp.service.dto.OmBaseInfoQueryCriteria;
import me.zhengjie.modules.cspimp.psp.service.mapstruct.OmBaseInfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
 * @author zmm
 * @website https://docs.auauz.net
 * @description 服务实现
 * @date 2020-05-30
 **/
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class OmBaseInfoServiceImpl implements OmBaseInfoService {

    private final OmBaseInfoRepository omBaseInfoRepository;
    private final OmBaseInfoMapper omBaseInfoMapper;

    @Override
    public Map<String, Object> queryAll(OmBaseInfoQueryCriteria criteria, Pageable pageable) {
        Page<OmBaseInfo> page = omBaseInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        return PageUtil.toPage(page.map(omBaseInfoMapper::toDto));
    }

    @Override
    public List<OmBaseInfoDto> queryAll(OmBaseInfoQueryCriteria criteria) {
        return omBaseInfoMapper.toDto(omBaseInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder)));
    }

    @Override
    public OmBaseInfoDto findById(Long id) {
        OmBaseInfo omBaseInfo = omBaseInfoRepository.findById(id).orElseGet(OmBaseInfo::new);
        ValidationUtil.isNull(omBaseInfo.getId(), "OmBaseInfo", "id", id);
        return omBaseInfoMapper.toDto(omBaseInfo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmBaseInfoDto create(OmBaseInfoVo resources) {
        OmBaseInfo omBaseInfo = new OmBaseInfo();
        BeanUtils.copyProperties(resources, omBaseInfo);
        omBaseInfo.setCid(0l);
        String regDateStr = resources.getRegDate();
        if (StringUtils.isNotBlank(regDateStr)) {
//            Date regDate = null;
//            try {
//                SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
//                regDate = sf.parse(regDateStr);
//            } catch (Exception e) {
//                e.getMessage();
//            }
            omBaseInfo.setRegDate(regDateStr);
        }
        omBaseInfoRepository.save(omBaseInfo);
        return omBaseInfoMapper.toDto(omBaseInfo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(OmBaseInfoVo resources) {
        OmBaseInfo omBaseInfo = omBaseInfoRepository.findById(resources.getId()).orElseGet(OmBaseInfo::new);
        ValidationUtil.isNull(omBaseInfo.getId(), "OmBaseInfo", "id", resources.getId());
        BeanUtils.copyProperties(resources, omBaseInfo);
        String regDateStr = resources.getRegDate();
        if (StringUtils.isNotBlank(regDateStr)) {
//            Date regDate = null;
//            try {
//                SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
//                regDate = sf.parse(regDateStr);
//            } catch (Exception e) {
//                e.getMessage();
//            }
            omBaseInfo.setRegDate(regDateStr);
        }
        omBaseInfoRepository.save(omBaseInfo);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            omBaseInfoRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<OmBaseInfoDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (OmBaseInfoDto omBaseInfo : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("创建者", omBaseInfo.getCreateBy());
            map.put("更新者", omBaseInfo.getUpdateBy());
            map.put("创建日期", omBaseInfo.getCreateTime());
            map.put("更新时间", omBaseInfo.getUpdateTime());
            map.put("组织名称", omBaseInfo.getName());
            map.put("营业执照", omBaseInfo.getBusinessLicense());
            map.put("行业", omBaseInfo.getIndustry());
            map.put("法人代表", omBaseInfo.getLegal());
            map.put("所属区域", omBaseInfo.getArea());
            map.put("性质", omBaseInfo.getNature());
            map.put("规模", omBaseInfo.getScale());
            map.put("联系人", omBaseInfo.getContactPerson());
            map.put("联系手机", omBaseInfo.getContactPhone());
            map.put("联系职务", omBaseInfo.getContactJob());
            map.put("联系电邮", omBaseInfo.getContactEmail());
            map.put("联系传真", omBaseInfo.getContactFax());
            map.put("联系固话", omBaseInfo.getContactTel());
            map.put("登记时间", omBaseInfo.getRegDate());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}